package com.xhwl.centiotdata.service.integrateconfig;

import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleIntegrationConfigBO;
import com.xhwl.common.pojo.cent.vehicle.converter.config.ToVehicleIntegrationConfigBO;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/12/17/ 11:45
 */
@Component
public class VehicleIntegrationConfig extends IntegrationConfig<Integer, List<VehicleIntegrationConfigBO>> {

    private Map<Integer, List<VehicleIntegrationConfigBO>> vehicleIntegrationConfigMap = new HashMap<>();

    @Override
    public List<VehicleIntegrationConfigBO> getIntegrationConfig(Integer projectId) {
        return vehicleIntegrationConfigMap.get(projectId);
    }

    public Map<Integer, List<VehicleIntegrationConfigBO>> getIntegrationConfigs() {
        return vehicleIntegrationConfigMap;
    }

    @EventListener
    public void listenIntegrationConfig(IntegrationConfigEvent event) {
        this.handleVehicleIntegrationConfig();
    }

    private void handleVehicleIntegrationConfig() {
        vehicleIntegrationConfigMap = IntegrationConfig.integrationConfigList.stream()
                .filter(deviceProjSupplierCategory -> CategoryEnum.YARD_GATE.getId().equals(deviceProjSupplierCategory.getProdCategoryId()))
                .map(ToVehicleIntegrationConfigBO::toToVehicleIntegrationConfigBO)
                .collect(Collectors.groupingByConcurrent(vehicleIntegrationConfigBO -> vehicleIntegrationConfigBO.getVehicleSystemInfoBO().getProjectId()));
    }

}
